﻿
message(STATUS "")
message(STATUS "#=======================================================#")
message(STATUS "#             START CONFIG FF_UTILS_TEST                #")
message(STATUS "#=======================================================#")

#=======================================================#
# 				    	test cases	 					#
#=======================================================#
set(FF_UTILS_TEST_CASE 
    alpha_test
    utils_test
    workspace_test
    thread_test
	producer_consumer_test
)
if(ENABLE_CUDA)
	list(APPEND FF_UTILS_TEST_CASE cuda_test)
	list(APPEND FF_UTILS_TEST_CASE cuda_kernel_test)
endif()
if(ENABLE_OPENGL)
	list(APPEND FF_UTILS_TEST_CASE opengl_test)
	list(APPEND FF_UTILS_TEST_CASE coordinate_test)
endif()
if(ENABLE_WINDOW)
	list(APPEND FF_UTILS_TEST_CASE window_test)
endif()
	
#=======================================================#
# 				    Find Libraries	 					#
#=======================================================#
find_library(LIB_FEIFEI ff_utils PATHS ${FFUTILS_LIBRARY_PATH})
message(STATUS "")
message(STATUS "include path = ${FFUTILS_INCLUDE_PATH}")
message(STATUS "library path = ${FFUTILS_LIBRARY_PATH}")
message(STATUS "LIB_FEIFEI = ${LIB_FEIFEI}")

foreach(TEST_CASE IN LISTS FF_UTILS_TEST_CASE)
	message(STATUS "")
	message(STATUS "Add test case: ${TEST_CASE}")
	
	#=======================================================#
	# 					Source File	 						#
	#=======================================================#
	if(${TEST_CASE} STREQUAL "cuda_kernel_test")
		set(TEST_CASE_SRC "${TEST_CASE}.cu")
	else()
		set(TEST_CASE_SRC "${TEST_CASE}.cpp")
	endif()
	add_executable(${TEST_CASE} 
		${FFUTILS_INCLUDE_PATH}/ff_utils.h
		${TEST_CASE_SRC}
	)
	message(STATUS "Add source file: ${TEST_CASE_SRC}")
	
	set_property(TARGET ${TEST_CASE} PROPERTY FOLDER utils_tests)
	
	#=======================================================#
	# 						Defines	 						#
	#=======================================================#
	target_compile_definitions(${TEST_CASE} PRIVATE TEST_WORK_SPACE=\"${CMAKE_CURRENT_SOURCE_DIR}/test_workspace/\")

	#=======================================================#
	# 				    Include Path	 					#
	#=======================================================#
	include_directories("${FFUTILS_INCLUDE_PATH}")
	
	#=======================================================#
	# 				    	Libraries	 					#
	#=======================================================#
	link_directories(${TEST_CASE} ${FFUTILS_LIBRARY_PATH})
	#target_link_libraries(${TEST_CASE} ${LIB_FEIFEI})
	if(UNIX)
		FIND_PACKAGE(Threads)
		TARGET_LINK_LIBRARIES(${TEST_CASE} ${CMAKE_THREAD_LIBS_INIT})
	endif()
	
	#=======================================================#
	# 				    	Dependent 	 					#
	#=======================================================#
	target_link_libraries(${TEST_CASE} ff_utils)

	#=======================================================#
	# 				    	OpenGL		 					#
	#=======================================================#
	if(ENABLE_OPENGL)
		find_package(OpenGL REQUIRED)
		target_include_directories(${TEST_CASE} PRIVATE ${OPENGL_INCLUDE_DIR})
		include_directories(${GLAD_DIR})
		include_directories(${GLM_DIR})
		include_directories(${GLFW_DIR}/include/)
		include_directories(${ASSIMP_DIR}/include/)
		include_directories(${STDIMG_DIR})
		target_link_libraries(${TEST_CASE} ${OPENGL_LIBRARIES})
		target_link_libraries(${TEST_CASE} ${LIB_GLFW})
		target_link_libraries(${TEST_CASE} ${LIB_ASSIMP})
	endif()
	
	#=======================================================#
	# 						CUDA	 						#
	#=======================================================#
	if(ENABLE_CUDA)		
		if(ENABLE_NVCC)
			target_sources(${TEST_CASE} PRIVATE ${FFUTILS_INCLUDE_PATH}/ff_utils_cuda.cuh)
		else()
			target_sources(${TEST_CASE} PRIVATE ${FFUTILS_INCLUDE_PATH}/ff_utils_cuda.h)
		endif()	
		
		set_target_properties(${TEST_CASE} PROPERTIES CUDA_ARCHITECTURES ${MY_CUDA_ARCHITECTURES})
		enable_language(CUDA)
		
		if(WIN32)
			find_package(CUDA)

			target_include_directories(${TEST_CASE} PRIVATE ${CUDA_INCLUDE_DIRS})
			link_directories(${TEST_CASE} ${CUDAToolkit_LIBRARY_DIR}/lib/x64/)
			target_link_libraries(${TEST_CASE} ${CUDA_LIBRARIES})
			target_link_libraries(${TEST_CASE} ${CUDA_CUFFT_LIBRARIES})
			
			# workaround for visual studio issue
			set(CMAKE_CUDA_STANDARD 17) 
		endif()

		if(UNIX)
			find_package(CUDAToolkit)

			target_include_directories(${TEST_CASE} PRIVATE ${CUDAToolkit_INCLUDE_DIRS})
			link_directories(${TEST_CASE} ${CUDAToolkit_LIBRARY_DIR})
			target_link_libraries(${TEST_CASE} CUDA::cudart_static)
			target_link_libraries(${TEST_CASE} CUDA::cufft_static)
		endif()
	endif()

	#=======================================================#
	# 					shell command						#
	#=======================================================#
	if(UNIX)
		message(STATUS "touch the ${FFUTILS_INCLUDE_PATH}/ff_utils.h")
		add_custom_command(TARGET ${TEST_CASE}
			PRE_BUILD
			COMMAND touch ${FFUTILS_INCLUDE_PATH}/ff_utils.h
		)	
	endif()
	
	#=======================================================#
	# 				    	add test	 					#
	#=======================================================#
	add_test(NAME FFLIB_${TEST_CASE} COMMAND ${TEST_CASE})

endforeach(TEST_CASE)



message(STATUS "#============= END CONFIG FF_UTILS_TEST ================#")
message(STATUS "")
